function results = runDraft(varargin)
c = clock;
diary off;
diary(sprintf('diary_%d_%d_%d_%d_%d_%d.txt',c(1),c(2),c(3),c(4),c(5),round(c(6))));
if nargin == 0
    rankings = parseRankings('2010_ESPN_Rankings.csv');
else
    rankings = varargin{1};
end
rankings.available = ones(size(rankings.rank));

players = {'bob','chad','chiu','buck','steve','mccubbin','cole','bill','todd','nate','pekala','stipes'};
%g - greedy, s - smart, r - random, h - human

checkCustoms(players);

RB = 1;
QB = 2;
WR = 3;
TE = 4;
K = 5;
DST = 6;
RWR = 7;
B = 8;

nPlayers = length(players);
roster = [QB RB RWR WR WR TE DST K B B B B];
nRounds = length(roster);
results = zeros(nRounds,nPlayers);


for i=1:nRounds
    for j=1:nPlayers
        if mod(i,2) == 1
            playerIndx = j;
        else
            playerIndx = nPlayers + 1 - j;
        end
        
        if i == 5 && strcmp(players{playerIndx},'chad')
            nate = 'cool';
        end
        
        %choose agent
        if strcmp(players{playerIndx},'g')
            fprintf('GREEDY: ');
            choice = greedyAgent(i,playerIndx,results,roster,rankings);
        elseif strcmp(players{playerIndx},'h')
            fprintf('HUMAN: ');
            choice = humanAgent(i,playerIndx,results,roster,rankings);
        elseif strcmp(players{playerIndx},'r')
            fprintf('RANDOM: ');
            choice = randomAgent(i,playerIndx,results,roster,rankings);
        elseif strcmp(players{playerIndx},'s')
            fprintf('SMART: ');
            choice = smartAgent(i,playerIndx,results,roster,rankings);
        else
            fprintf('%s: ',players{playerIndx});
            choice = customAgent(players{playerIndx},i,playerIndx,results,roster,rankings);
        end
    
        fprintf('  chooses %s\n',playerName(rankings,choice));
        
        if strcmp(players{playerIndx},'h')
            nate = 'cool';
        end
        
        if isAvailable(rankings,choice)
            rankings = makeChoice(rankings,choice);
            results(i,playerIndx) = choice;
        else
            fprintf('  bad choice\n');
            for k=1:length(rankings.rank)
                if rankings.available(k)
                    rankings.available(k) = 0;
                    results(i,playerIndx) = k;
                    break;
                end
            end
        end
    end
    
end

fprintf('\nFinal Rosters:\n');
for j=1:length(players)
    fprintf('\n%s:\n',players{j});
    for i=1:size(results,1)
        cName = rankings.name(results(i,j));
        fprintf('%s\n',cName{1});
    end
end

diary off
end

function rez = isAvailable(rankings,choice)
    i = rankings.id == choice;
    rez = rankings.available(i);
end

function rankings = makeChoice(rankings,choice)
    i = rankings.id == choice;
    rankings.available(i) = 0;
end

function name = playerName(rankings,choice)
    cName= rankings.name(choice);
    name = cName{1};
end

function checkCustoms(players)
    d = dir;
    found = zeros(1,length(players));
    fprintf('Custom Players:\n');
    for i=1:length(players)
        for j=1:length(d)
            if strcmp(strcat(players{i},'.csv'), d(j).name)
                found(i) = 1;
                break;
            end
        end
        fprintf('%s - %d\n',players{i}, found(i));
    end
    
    c = input('Is this correct (1/0)?');
    if c ~= 1
        error('OK QUITTING');
    end
end